<?php
class app_keu_model extends MY_Model {
	var $model = 'app';
	
	function CheckLogin($as_userid, $as_password){
		$result = $this->db->get_where('IT_USERID', array('USER_ID' => strtoupper($as_userid), 'STATUS' => '1'));
		
		if($result->num_rows() == 1){
			$arr_data	= $result->row_array();
			if(strtoupper($as_password) == strtoupper(decrypt($arr_data['user_password'],$this->config->item('encryption_key')))){
				return TRUE;
			}else{
				return FALSE;
			}
		}else{
			return FALSE;
		}
	}
	
		
	function EmailConfig(){
		$config['protocol']		=$this->web_option("email_protocol");
		$config['smtp_host']	=$this->web_option("smtp_host"); 
		$config['smtp_port']	=$this->web_option("smtp_port"); 
		$config['smtp_timeout']	=$this->web_option("smtp_timeout"); 
		$config['smtp_user']	=$this->web_option("smtp_user"); 
		$config['smtp_pass']	=decrypt($this->web_option("smtp_pass"),$this->config->item('encryption_key')); 
		$config['charset']		='utf-8';  
		$config['newline']		="\r\n";  
		$config['mailtype'] 	= 'html';
		
		return $config;
	}
	
	function CreateSession($row)
	{
		// Delete Active Session
		$this->db->delete('it_session', array('owner' => $row['owner'])); 

		// Update Last Login
		$this->db->update('it_userid', array('last_login' => NOW), array('user_id' => $row['owner']));

		// Insert Session
		$this->db->insert('it_session', $row);
	}

	function UpdateSession($as_userid)
	{
		$ls_session_expired = $this->web_option("SESSION_EXPIRED");
		$SESSION_EXPIRED = ($ls_session_expired == "" ? 30 : intval($ls_session_expired));

		// Update Active Session
		$this->db->update(	'it_session', 
							array('expired' => @date("Y-m-d H:i:s", strtotime("+".$SESSION_EXPIRED." minutes"))), 
							array('owner' => $as_userid));
	}

	function RefreshSession()
	{
		$this->db->where('expired < '.strtotime(@date("Y-m-d H:i:s")));
		$this->db->delete('it_session');
	}
	
	function GetUserSessionByOwner($as_userid){
		$result = $this->db->get_where('it_session', array('owner' => $as_userid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetUserSessionBySessid($as_sessid)
	{
		$result = $this->db->get_where('it_session', array('sessid' => $as_sessid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function UserIsDeveloper($as_userid)
	{
		$result = $this->db->get_where('it_user_role', array('user_id' => $as_userid, 'role_id' => 'DEV'));
		if($result->num_rows() == 1) return TRUE; else return FALSE;
	}
	
	function GetApplication()
	{
		//$this->db->select('it_application.*');
		//$this->db->from('it_application');
		//$this->db->where("APP_ID <> 'CORE'");
		//$this->db->order_by('ORDER_NO','asc');
		//$result = $this->db->get();
		
		$result = $this->db->query("SELECT APP.*
									  FROM IT_APPLICATION APP
										JOIN IT_USER_APPLICATION USRAPP ON APP.APP_ID = USRAPP.APP_ID
									 WHERE USRAPP.USER_ID = '".USERID."'
									 ORDER BY APP.APP_NAME");
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			return $arr_data;
		}else{
			return array();
		}
	}

	function GetUserGetRole($as_userid)
	{
		$this->db->select('it_role.*');
		$this->db->from('it_role');
		$this->db->join('it_user_role', 'it_role.role_id = it_user_role.role_id');
		$this->db->where('user_id', $as_userid);
		$this->db->where('app_id', APP);
		$this->db->where('it_user_role.role_id <>', 'DEV');
		
		$result = $this->db->get();
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			return $arr_data;
		}else{
			return array();
		}
	}
	
	function GetUserDefaultRole($as_userid)
	{
		$this->db->from('it_user_role');
		$this->db->where('user_id', $as_userid);
		
		$result = $this->db->get();
		
		if($result->num_rows() > 0){
			$row	= $result->row_array();
			return $row['role_id'];
		}else{
			return '';
		}
	}
	
	function GetUserDefaultApp($as_userid)
	{
		$result	= $this->db->query("SELECT	b.app_id 
									FROM	it_user_role a 
											JOIN it_role b ON a.role_id=b.role_id
									WHERE	a.USER_ID = '".$as_userid."'");
		if($result->num_rows() > 0){
			$row	= $result->row_array();
			return $row['app_id'];
		}else{
			return 'CORE';
		}
	}
}
?>